package com.chagee.store.operation.server.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.chagee.base.utils.SoaResponse;
import com.chagee.base.utils.SoaUtil;
import com.chagee.personnel.api.event.bo.PersonnelInfo;
import com.chagee.personnel.api.event.request.*;
import com.chagee.personnel.api.event.response.*;
import com.chagee.personnel.api.event.service.emp.ChageeEmpReadApiService;
import com.personnel.auth.service.PersonnelAuthService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author : hk
 * @Date : 2024/7/10
 */

@Service
@Slf4j
public class PersonnelThirdService {

	@Resource
	private PersonnelAuthService personnelAuthService;

	@Resource
	private ChageeEmpReadApiService chageeEmpReadApiService;

	public PersonnelInfo getLoginUserInfoInWeb(){
		try {
			log.info("PersonnelThirdService#getLoginUserInfoInWeb...");
			PersonnelInfo loginUser = personnelAuthService.getLoginUserInfoInWeb();
			return loginUser;
		} catch (Exception e) {
			log.error("PersonnelThirdService#getLoginUserInfoInWeb,error:",e);
			return null;
		}
	}

	public List<ListByStoreNoListResp> listByStoreNoList(List<String> storeNos) {

		log.info("chageeEmpReadApiService_listByStoreNoList,request: {}", JSONObject.toJSONString(storeNos));
		if (CollectionUtil.isEmpty(storeNos)) {
			return new ArrayList<>();
		}
		try {
			ListByStoreNoListReq request = new ListByStoreNoListReq();
			request.setStoreNos(storeNos);
			SoaResponse<List<ListByStoreNoListResp>, Void> listVoidSoaResponse = chageeEmpReadApiService.listByStoreNoList(request);
			log.info("chageeEmpReadApiService_listByStoreNoList,response: {}", JSONObject.toJSONString(listVoidSoaResponse.getResponseVo()));
			return listVoidSoaResponse.getResponseVo();
		} catch (Exception e) {
			log.error("chageeEmpReadApiService_listByStoreNoList, error: ",e);
			return null;
		}

	}

	public List<EmployeeResp> getChageeUserDetailList(Collection<?> userIds) {
		if (CollectionUtils.isEmpty(userIds)) {
			return Collections.emptyList();
		}
		EmployeeQueryByIdReq req = new EmployeeQueryByIdReq();
		List<Long> userList = userIds.stream().map(t -> {
			if (t instanceof String) {
				if (StringUtils.isBlank((String) t)) {
					return null;
				}
				return Long.valueOf((String) t);
			} else {
				if (Objects.isNull(t)) {
					return null;
				}
				return (Long) t;
			}
		}).filter(Objects::nonNull).distinct().collect(Collectors.toList());
		if (CollectionUtils.isEmpty(userList)) {
			return Collections.emptyList();
		}
		req.setUserIds(userList);
		req.setIsQueryDeptInfo(false);
		SoaResponse<List<EmployeeResp>, Void> response = chageeEmpReadApiService.getChageeUserDetailList(req);
		return SoaUtil.unpack(response);

	}


}
